package com.tempjob.common.utils;

import com.google.common.base.Optional;
import org.springframework.beans.BeanUtils;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.cglib.beans.BeanMap;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @Author: jiuhui.ye
 * @Date: 2019/5/17 11:47
 */
public class CachedBeanCopier {
    static final Map<String, BeanCopier> BEAN_COPIERS = new HashMap<String, BeanCopier>();

    /**
     * 不使用转换器拷贝方法
     *
     * @param srcObj  the src obj
     * @param destObj the dest obj
     * @return void
     * @throws
     */
    public static void copy(Object srcObj, Object destObj) {
        if (srcObj == null) {
            destObj = null;
            return;
        }
        String key = genKey(srcObj.getClass(), destObj.getClass());
        BeanCopier copier = null;
        if (!BEAN_COPIERS.containsKey(key)) {
            copier = BeanCopier.create(srcObj.getClass(), destObj.getClass(), false);
            BEAN_COPIERS.put(key, copier);
        } else {
            copier = BEAN_COPIERS.get(key);
        }
        copier.copy(srcObj, destObj, null);
    }

    /**
     * Copy list.
     *
     * @param srcList  the src list
     * @param destList the dest list
     */
    public static <S, D> void copyList(List<S> srcList, List<D> destList, Class<D> c) {
        try {
            if (srcList != null && destList != null) {
                for (S src : srcList) {
                    D dest = c.newInstance();
                    copy(src, dest);
                    destList.add(dest);
                }
            } else {
                throw new IllegalArgumentException("参数不能为空");
            }
        } catch (Exception e) {
            throw new IllegalArgumentException(e.getMessage());
        }
    }


    // 对象考贝
    public static void copyObject(Object source, Object target) {
        BeanUtils.copyProperties(source, target);
    }

    /**
     * POJO转换Map
     *
     * @param srcObj
     * @return
     * @return Map<String, Object>
     * @throws
     */
    @SuppressWarnings("unchecked")
    public static Map<String, Object> pojo2Map(Object srcObj) {
        Map<String, Object> destMap = new HashMap<String, Object>();
        if (Optional.fromNullable(srcObj).isPresent()) {
            BeanMap beanMap = BeanMap.create(srcObj);
            Iterator<String> it = beanMap.keySet().iterator();
            while (it.hasNext()) {
                String key = it.next();
                Object obj = beanMap.get(key);
                if (Optional.fromNullable(obj).isPresent()) {
                    destMap.put(key, beanMap.get(key));
                }
            }
        }
        return destMap;

    }

    /**
     * 自定义对象转换器
     *
     * @param @param srcObj		源对象
     * @param @param destObj		目标对象
     * @param @param converter 	自定义对象转换器 （需实现net.sf.cglib.core.Converter接口）
     * @return void
     * @throws
     */
//    public static void copyByConverter(Object srcObj, Object destObj , Converter converter) {
//        if(srcObj == null){
//            destObj = null;
//            return ;
//        }
//        String key = genConverterKey(srcObj.getClass(), destObj.getClass(),converter.getClass());
//        BeanCopier copier = null;
//        if (!BEAN_COPIERS.containsKey(key)) {
//            copier = BeanCopier.create(srcObj.getClass(), destObj.getClass(), true);
//            BEAN_COPIERS.put(key, copier);
//        } else {
//            copier = BEAN_COPIERS.get(key);
//        }
//        copier.copy(srcObj, destObj, converter);
//    }
    private static String genKey(Class<?> srcClazz, Class<?> destClazz) {
        return srcClazz.getName() + destClazz.getName();
    }

    private static String genConverterKey(Class<?> srcClazz, Class<?> destClazz, Class<?> converterClazz) {
        return srcClazz.getName() + destClazz.getName() + converterClazz.getName();
    }
}
